package com.gxa.jjty.admin.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.AppServeService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.AppServe;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Set;

/**
 * @author asus
 */
@Service
@Transactional
public class AppServeServiceImpl extends BaseServiceImpl<AppServe> implements AppServeService {

    @Autowired
    RedisTemplate<String,Object> redisTemplate ;

    @Autowired
    Mapper<AppServe> mapper;

    ListOperations<String,Object> listOps = null;



    @Override
    public void insertApp(AppServe appServe) throws BusinessException {
        listOps = redisTemplate.opsForList();
        int i = mapper.insertSelective(appServe);
        if (i != 1){
           throw  new BusinessException(ExceptionCode.ADD_DEFEATED);
        }

        Set<String> keys = redisTemplate.keys("serveList:*");
        if ((keys != null ? keys.size() : 0) >0){
            redisTemplate.delete(keys);
        }
    }


    @Override
    public void updateAppServe(AppServe appServe) throws BusinessException {
        listOps = redisTemplate.opsForList();
        Long id = appServe.getId();
        AppServe appServe1 = mapper.selectByPrimaryKey(id);
        if (appServe1 != null){
            int i = mapper.updateByPrimaryKeySelective(appServe);
            if (i != 1){
                throw  new BusinessException(ExceptionCode.UPDATE_FALSE);
            }
            Set<String> keys = redisTemplate.keys("serveList:*");
            if ((keys != null ? keys.size() : 0) >0){
                redisTemplate.delete(keys);
            }
        }else {
           throw new  BusinessException(ExceptionCode.APP_NO_EXIST);
        }

    }


    @Override
    public  void deleteAppServe(Long id) throws BusinessException {
        AppServe appServe = mapper.selectByPrimaryKey(id);
        Set<String> keys = redisTemplate.keys("serveList:*");
        if ((keys != null ? keys.size() : 0) >0){
            redisTemplate.delete(keys);
        }
        int i = mapper.deleteByPrimaryKey(id);
        if (i != 1){
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }
    }

    @Override
    public PageInfo<AppServe> selectByPage(Integer page, Integer size, String appName, Integer appStatus) {
        PageHelper.startPage(page,size);
        Example example = new Example(AppServe.class);
        Example.Criteria criteria = example.createCriteria();
        //select...where name like''
        if (appName != null  && appName != ""){
              criteria.andLike("appName", "%"+appName+"%");
        }
        if (appStatus !=null){
            System.out.println(criteria);
            criteria.andEqualTo("appStatus",appStatus);
        }
        List<AppServe> appServes = mapper.selectByExample(example);
        PageInfo<AppServe> of = PageInfo.of(appServes);
        return  of;
    }


}
